package cn.tp.ksoft.civil_driver.dialog;

/**
 * Created by zhusong on 16/5/11.
 */

import android.app.Dialog;
import android.content.Context;
import android.database.DataSetObserver;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import cn.tp.ksoft.library_civil.widget.wheel.WheelView;
import cn.tp.ksoft.library_civil.widget.wheel.WheelViewAdapter;
import cn.tp.ksoft.civil_driver.R;

/**
 * Created by zhusong on 16/5/2.
 */
public abstract class WheelDialog extends Dialog implements View.OnClickListener{

    protected DialogCallBack mCallBack;
    protected TextView mTitleTv;
    protected WheelView mWheel1;
    protected WheelView mWheel2;
    protected WheelView mWheel3;
    protected ItemAdapter mWheelAdapter1;
    protected ItemAdapter mWheelAdapter2;
    protected ItemAdapter mWheelAdapter3;

    public WheelDialog(Context context) {
        this(context, R.style.style_common_custom_progress_dialog);
    }

    public WheelDialog(Context context, int themeResId) {
        super(context, themeResId);
        setContentView(R.layout.driver_dialog_wheelview);
        mTitleTv = (TextView) findViewById(R.id.tv_title);
        findViewById(R.id.btn_cancel).setOnClickListener(this);
        findViewById(R.id.btn_ok).setOnClickListener(this);
        mWheel1 = (WheelView) findViewById(R.id.wheel1);
        mWheel2 = (WheelView) findViewById(R.id.wheel2);
        mWheel3 = (WheelView) findViewById(R.id.wheel3);

        mWheelAdapter1 = new ItemAdapter(null);
        mWheelAdapter2 = new ItemAdapter(null);
        mWheelAdapter3 = new ItemAdapter(null);

        mWheel1.setViewAdapter(mWheelAdapter1);
        mWheel2.setViewAdapter(mWheelAdapter2);
        mWheel3.setViewAdapter(mWheelAdapter3);

        initData();
    }

    protected abstract void initData();
    public void setCallBack(DialogCallBack callBack){
        this.mCallBack = callBack;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel : {
                dismiss();
                onNegativeClick();
                break;
            }

            case R.id.btn_ok : {
                dismiss();
                onPositiveClick();
                break;
            }
        }
    }

    protected void onNegativeClick() {
        if(null != mCallBack) {
            mCallBack.onNegativeClick();
        }
    }

    protected void onPositiveClick() {
        if(null != mCallBack) {
            mCallBack.onPositiveClick();
        }
    }

    class ItemAdapter implements WheelViewAdapter {
        private SparseArray<String> mData;
        public ItemAdapter(SparseArray<String> data) {
            mData = data;
        }

        public int getKey(int index) {
            if (null != mData) {
                return mData.keyAt(index);
            }
            return 0;
        }

        public String getValue(int index) {
            if (null != mData) {
                return mData.valueAt(index);
            }
            return null;
        }
        public void setData(SparseArray<String> data) {
            mData = data;
        }

        @Override
        public int getItemsCount() {
            return null != mData ? mData.size() : 0 ;
        }

        @Override
        public View getItem(int index, View convertView, ViewGroup parent) {
            String displayName = null;
            if (null != mData) {
                displayName = getValue(index);
            }
            TextView textView = (TextView) LayoutInflater.from(getContext())
                    .inflate(R.layout.driver_wheel_item, parent, false);
            textView.setText(displayName);
            return textView;
        }

        @Override
        public View getEmptyItem(View convertView, ViewGroup parent) {
            return null;
        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {

        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {

        }
    }
}
